#!/usr/bin/env python
# coding=utf-8

import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import math
import urllib2
import httplib
from time import strftime, localtime
from Route import *

url = "http://vacations.ctrip.com/Booking/WebService/PkgListWebservice.asmx"
SM_TEMPLATE = """<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
   <soap:Header/>
   <soap:Body>
      <tem:GetKaiXinPkgList>
         <!--Optional:-->
         <tem:security>
            <!--Optional:-->
            <tem:UserAccount>oyesgo</tem:UserAccount>
            <!--Optional:-->
            <tem:Password>oyesgo805</tem:Password>
         </tem:security>
         <tem:startCity>%s</tem:startCity>
         <tem:searchType>%s</tem:searchType>
         <!--Optional:-->
         <tem:searchValue></tem:searchValue>
      </tem:GetKaiXinPkgList>
   </soap:Body>
</soap:Envelope>"""

pkg_re = re.compile(r'''<Pkg>([\s\S]+?)</Pkg>''',re.X|re.S)


vacation_info_re_pre = re.compile(r'''
        <Pkg>(.*?)<\s*/\s*Pkg>
        ''', re.X|re.S)
xml_cdata_node_re = re.compile(r'''
        <\s*(\w+)\s*>\s*(.*?)\s*<\s*/\s*\1\s*>
        ''', re.X|re.S)

depcity_date = [ 
              ('1','北京'),
              ('2','上海'),
              ('3','天津'),
              ('4','重庆'),
              ('5','哈尔滨'),
              ('6','大连'),
              ('7','青岛'),
              ('10','西安'),
              ('12','南京'),
              ('13','无锡'),
              ('17','杭州'),
              ('25','厦门'),
              ('28','成都'),
              ('30','深圳'),
              ('32','广州'),
              ('34','昆明'),
              ('38','贵阳'),
              ('39','乌鲁木齐'),
              ('100','兰州'),
              ('103','呼和浩特'),
              ('105','太原'),
              ('144','济南'),
              ('158','长春'),
              ('206','长沙'),
              ('258','福州'),
              ('278','合肥'),
              ('375','宁波'),
              ('376','南昌'),
              ('380','南宁'),
              ('451','沈阳'),
              ('477','武汉'),
              ('491','温州'),
              ('559','郑州') ]

#wsdlObject = WSDL.Proxy(url + '?wsdl')
#ci = wsdlObject.methods['']

def do_crawl():
    for depcity_id,depcity in depcity_date:
        
        SoapMessage = SM_TEMPLATE%(depcity_id,"U")
        print SoapMessage
        webservice = httplib.HTTP("vacations.ctrip.com")
        webservice.putrequest("POST", "/Booking/WebService/PkgListWebservice.asmx")
        webservice.putheader("Host", "vacations.ctrip.com")
        webservice.putheader("User-Agent", "Python post")
        webservice.putheader("Content-type", "text/xml; charset=\"utf-8\"")
        webservice.putheader("Content-length", "%d" % len(SoapMessage))
        webservice.putheader("SOAPAction", "http://tempuri.org/GetKaiXinPkgList")
        webservice.endheaders()
        webservice.send(SoapMessage)
        
        statuscode, statusmessage, header = webservice.getreply()
        print "Response: ", statuscode, statusmessage
        print "headers: ", header
        res = webservice.getfile().read()
        vacation_info_list = vacation_info_re_pre.findall(res)
        print len(vacation_info_list)
        for vacation_info in vacation_info_list:
            vacation_info_dict = dict(xml_cdata_node_re.findall(vacation_info))
            if not vacation_info_dict:
                print >>sys.stderr, 'cannot match xml_cdata_node_re'
                continue
    
            v = vacation_info_dict
            route = Route()
            route.site = '55'        
            route.supplier = '携程旅游网'
            route.supplier_url = 'www.ctrip.com'
            route.go_t = route.back_t = "飞机"
            route.type = '常规线路'
            route.outcity = depcity
            route.dates = strftime("%Y-%m-%d",localtime()) + ";"
            route.title = v.get('PkgName', '')
            route.days = v.get('TripDays', '')
            route.price = v.get('MinPrice', '')
            route.img_url = v.get('FirstImageURI', '')
            route.detail = v.get('ShortDesc', '')
            route.rout_url = v.get('URI', '')
            route.updRoute()
            print >>sys.stderr, 'updating', route.supplier, route.title
            
    


if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    do_crawl()
   

